﻿using System.Data;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.HtmlControls;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

/// <summary>
///dbmanager 的摘要说明
/// </summary>


public class dbmanagerSql
{

    public dbmanagerSql()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    //public static string strconn = CommitGlsr.Program.gs_sqlConn; //获取数据库
    public static DataTable ExecuteQuerySql(string strconn, string strSQL)
    {
        SqlConnection conn = new SqlConnection(strconn);
        try
        {
            SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            return ds.Tables[0];
        }

        finally
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }

    ////public static string strconn2 = CommitGlsr.Program.gs_sqlConn2; //获取数据库
    //public static DataTable ExecuteQuerySql2(string strconn,string strSQL)
    //{
    //    SqlConnection conn2 = new SqlConnection(strconn2);
    //    try
    //    {
    //        SqlDataAdapter adapter = new SqlDataAdapter(strSQL, conn2);
    //        DataSet ds = new DataSet();
    //        adapter.Fill(ds);
    //        return ds.Tables[0];
    //    }
    //
    //    finally
    //    {
    //        if (conn2.State == ConnectionState.Open)
    //            conn2.Close();
    //    }
    //}
    /// <summary>
    /// 执行对ACCESS数据库的插入、删除或修改操作
    /// </summary>
    /// <param name="strSQL">插入、删除或修改的SQL语句</param>
    /// <returns>返回插入、删除或修改的SQLyuju 所影响的行数</returns>
    public static int ExecuteNonQuerySql(string strconn, string strSQL)
    {

        //  string strconn = "Server=58.247.32.214,9480;Initial Catalog=iotIS;User ID=sa;Password=password";
        //string strconn = "Server=10.0.0.213;Initial Catalog=iotIS;User ID=sa;Password=password";
        SqlConnection conn = new SqlConnection(strconn);
        conn.Open();
        try
        {
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            return (cmd.ExecuteNonQuery());
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }

    //  public static int ExecuteNonQuerySql2(string strSQL)
    //  {
    //
    //      //  string strconn = "Server=58.247.32.214,9480;Initial Catalog=iotIS;User ID=sa;Password=password";
    //      //string strconn = "Server=10.0.0.213;Initial Catalog=iotIS;User ID=sa;Password=password";
    //      SqlConnection conn2 = new SqlConnection(strconn2);
    //      conn2.Open();
    //      try
    //      {
    //          SqlCommand cmd = new SqlCommand(strSQL, conn2);
    //          return (cmd.ExecuteNonQuery());
    //      }
    //      finally
    //      {
    //          if (conn2.State == ConnectionState.Open)
    //              conn2.Close();
    //      }
    //  }
    public static object ExecuteScalarSql(string strconn, string strSQL)
    {

        //string strconn = "Server=58.247.32.214,9480;Initial Catalog=iotIS;User ID=sa;Password=password";
        //string strconn = "Server=10.0.0.213;Initial Catalog=iotIS;User ID=sa;Password=password";
        SqlConnection conn = new SqlConnection(strconn);
        conn.Open();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = strSQL;
            return cmd.ExecuteScalar();
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }
}
